package com.mls.fivedistrict.config;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 生成代码
 * Created by chenxiuxiang on 2018/4/22.
 */
public class GeneratorServiceEntity {
    public static void generateCode() {
        String packageName = "com.mls.fivedistrict";
        boolean serviceNameStartWithI = false;
        //user -> UserService, 设置成true: user -> IUserService
        //  generateByTables(serviceNameStartWithI, packageName, "t_role", "t_user_role","t_application","t_vote",
        // "t_vote_option","t_user_vote","t_vote_option_pay_integral","t_user_vote_option","t_news_classify","t_news","t_news_comment","t_like_record","t_news_read_report","t_news_share_report",
        // "t_goods_classify","t_goods","t_goods_pay_integral","t_goods_order","t_good_advertising","t_logistics_company");
        generateByTables(serviceNameStartWithI, packageName, "t_user");
    }

    private static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://47.97.203.252:3306/five_5_10";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("server123456")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                // 全局大写命名
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                //修改替换成你需要的表名，多个表名传数组
                .setInclude(tableNames)
                .setTablePrefix(new String[]{"t_"});
        config.setActiveRecord(true)
                // XML ResultMap
                .setBaseResultMap(true)
                // XML columList
                .setBaseColumnList(true)
                .setAuthor("Five Team")
                .setOutputDir("codeGen")
                .setEnableCache(false)
                .setFileOverride(true);

        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setMapper("dao")
                                .setXml("dao.xml")
                                .setService("server")
                                .setServiceImpl("server.impl")
                                .setEntity("pojo")
                ).execute();
    }

    private static void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }

    public static void main(String[] args) {

        generateCode();
    }
}
